package com.logistics.service.impl;

import com.logistics.common.Result;
import com.logistics.entity.TransportRecord;
import com.logistics.mapper.TransportRecordMapper;
import com.logistics.service.TransportRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class TransportRecordServiceImpl implements TransportRecordService {
    @Autowired
    private TransportRecordMapper transportRecordMapper;

    @Override
    @Transactional
    public Result<TransportRecord> createTransport(TransportRecord record) {
        try {
            // 生成运输单号
            record.setTransportNo(generateTransportNo());
            record.setStatus("待发车");
            
            transportRecordMapper.insert(record);
            return Result.success(record);
        } catch (Exception e) {
            return Result.error("创建运输记录失败：" + e.getMessage());
        }
    }

    @Override
    public Result<TransportRecord> getTransport(Long id) {
        try {
            TransportRecord record = transportRecordMapper.selectById(id);
            if (record == null) {
                return Result.error("运输记录不存在");
            }
            return Result.success(record);
        } catch (Exception e) {
            return Result.error("获取运输记录失败：" + e.getMessage());
        }
    }

    @Override
    public Result<TransportRecord> getTransportByTransportNo(String transportNo) {
        try {
            TransportRecord record = transportRecordMapper.selectByTransportNo(transportNo);
            if (record == null) {
                return Result.error("运输记录不存在");
            }
            return Result.success(record);
        } catch (Exception e) {
            return Result.error("获取运输记录失败：" + e.getMessage());
        }
    }

    @Override
    public Result<List<TransportRecord>> getTransportsByOrderId(Long orderId) {
        try {
            List<TransportRecord> records = transportRecordMapper.selectByOrderId(orderId);
            return Result.success(records);
        } catch (Exception e) {
            return Result.error("获取运输记录失败：" + e.getMessage());
        }
    }

    @Override
    public Result<List<Map<String, Object>>> listTransports() {
        try {
            List<TransportRecord> records = transportRecordMapper.selectList(null);
            List<Map<String, Object>> result = new ArrayList<>();
            
            for (TransportRecord record : records) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", record.getId());
                map.put("transportNo", record.getTransportNo());
                map.put("orderId", record.getOrderId());
                map.put("transportType", record.getTransportType());
                map.put("currentLocation", record.getCurrentLocation());
                map.put("status", record.getStatus());
                map.put("createTime", record.getCreateTime());
                result.add(map);
            }
            
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("获取运输记录列表失败：" + e.getMessage());
        }
    }

    @Override
    public Result<List<Map<String, Object>>> searchTransports(String transportNo, String status, String startDate, String endDate) {
        try {
            List<TransportRecord> records = transportRecordMapper.search(transportNo, status, startDate, endDate);
            List<Map<String, Object>> result = new ArrayList<>();
            
            for (TransportRecord record : records) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", record.getId());
                map.put("transportNo", record.getTransportNo());
                map.put("transportType", record.getTransportType());
                map.put("currentLocation", record.getCurrentLocation());
                map.put("status", record.getStatus());
                map.put("createTime", record.getCreateTime());
                result.add(map);
            }
            
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("搜索运输记录失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<TransportRecord> updateTransport(TransportRecord record) {
        try {
            // 检查运输记录是否存在
            TransportRecord existRecord = transportRecordMapper.selectById(record.getId());
            if (existRecord == null) {
                return Result.error("运输记录不存在");
            }
            
            transportRecordMapper.update(record);
            return Result.success(record);
        } catch (Exception e) {
            return Result.error("更新运输记录失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<Void> deleteTransport(Long id) {
        try {
            // 检查运输记录是否存在
            TransportRecord record = transportRecordMapper.selectById(id);
            if (record == null) {
                return Result.error("运输记录不存在");
            }
            
            transportRecordMapper.deleteById(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除运输记录失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<TransportRecord> updateTransportStatus(Long id, String status) {
        try {
            // 检查运输记录是否存在
            TransportRecord record = transportRecordMapper.selectById(id);
            if (record == null) {
                return Result.error("运输记录不存在");
            }
            
            transportRecordMapper.updateStatus(id, status);
            record.setStatus(status);
            return Result.success(record);
        } catch (Exception e) {
            return Result.error("更新运输记录状态失败：" + e.getMessage());
        }
    }

    // 生成运输单号
    private String generateTransportNo() {
        return "TR" + System.currentTimeMillis() + UUID.randomUUID().toString().substring(0, 6);
    }
} 